package com.yupi.springbootinit.controller;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.yupi.springbootinit.bizmq.BiMqConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/rebbit")
@Slf4j
public class RebbitController {

    @GetMapping("/add")
    public void addRebbitQueue(){
        try {
            log.info("开始创建队列");
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("localhost");
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();
            String EXCHANGE_NAME = BiMqConstant.WENXIN_EXCHANGE_NAME;
            channel.exchangeDeclare(EXCHANGE_NAME, "direct");

            //创建队列，随机分配一个队列名称
            String queueName = BiMqConstant.WENXIN_QUEUE_NAME;
            channel.queueDeclare(queueName, true, false, false, null);
            channel.queueBind(queueName, EXCHANGE_NAME, BiMqConstant.WENXIN_ROUTING_KEY);



            String EXCHANGE_NAME2 = BiMqConstant.BI_EXCHANGE_NAME;
            channel.exchangeDeclare(EXCHANGE_NAME2, "direct");
            //创建队列，随机分配一个队列名称
            String queueName2 = BiMqConstant.BI_EXCHANGE_NAME;
            channel.queueDeclare(queueName2, true, false, false, null);
            channel.queueBind(queueName2, EXCHANGE_NAME2, BiMqConstant.BI_ROUTING_KEY);
        } catch (Exception e) {
            log.error("创建队列失败!!");
        }
    }
}
